<template>
	<view class="warpper">
		<view class="img_container">
			<image :show-menu-by-longpress='true' style="width: 100%;height:100%;border: none;" :src="imgUrl"></image>
		</view>
		<view class="refresh">
			<view class="btn" @click="refresh()">
				刷新
			</view>
		</view>
		<view class="input_container">
			<u-input placeholder="请填写条形码内容" v-model="options.code" @input="inputHandle()"></u-input>
		</view>
		<view class="setting">
			<view class="btn" @click="setHandle()">
				条形码样式设置
			</view>
		</view>
		<view class="barcode_container">
			<w-barcode @longpress="longtap" ref="barcode" :options="options" @generate="aleard"
				@error="handleError"></w-barcode>
		</view>

		<u-toast ref="uToast" />

		<u-popup v-model="settingFlag" mode="bottom" border-radius="30" :mask="false" >
			<view class="set_content">
				<scroll-view scroll-y="true" style="height: 900rpx;">
					<view class="set_item">
						<view class="set_title">
							<view class="title_label">
								是否显示文字：
							</view>
							<view class="title_content">
								<u-switch v-model="showText" @change="textSwitch()"></u-switch>
							</view>
						</view>
					</view>
					<view class="set_item">
						<view class="set_title">
							<view class="title_label">
								前景色：
							</view>
							<view class="title_content">
								背景色设置为浅色，条形码设置为深色更易识别
							</view>
						</view>
						<view class="item_content">
							<view class="color_li" v-for="(item,index) in barColorList" :key="index"
								:style="{backgroundColor:item.value,}" :class="{'shadow':item.flag}"
								@click="barColorHandle(item.value)">
							</view>
						</view>
					</view>
					<view class="set_item">
						<view class="set_title">
							<view class="title_label">
								背景色：
							</view>
							<view class="title_content">
								背景色设置为浅色，条形码设置为深色更易识别
							</view>
						</view>
						<view class="item_content">
							<view class="color_li" v-for="(item,index) in bgcolorList" :key="index"
								:class="{'shadow':item.flag}" :style="{backgroundColor:item.value}"
								@click="bgHandle(item.value)">
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</u-popup>
	</view>

</template>
<script>
	export default {
		data() {
			return {
				options: {
					color: ['#000000'], // 条形码的颜色 默认黑色
					bgColor: '#FFFFFF', // 背景色
					width: 670, // 宽度 单位rpx
					height: 100, // 高度 单位rpx
					code: 'E57890543271985', // 生成条形码的值
					text: {
						content: '',
						color: ['#000'],
						position: 'bottom',
						size: 36,
						padding: 6
					}
				},
				imgUrl: "",
				settingFlag: false,
				showText: false,
				//条形码颜色
				barColorList: [{
						value: "#000000",
						flag: true
					},
					{
						value: "#365f93",
						flag: false
					},
					{
						value: "#034a9e",
						flag: false
					},
					{
						value: "#013479",
						flag: false
					},
					{
						value: "#4a90e2",
						flag: false
					},
					{
						value: "#595959",
						flag: false
					},
					{
						value: "#617891",
						flag: false
					},
					{
						value: "#6b5072",
						flag: false
					},
					{
						value: "#005441",
						flag: false
					},
					{
						value: "#417505",
						flag: false
					},
					{
						value: "#04777a",
						flag: false
					},
					{
						value: "#af1419",
						flag: false
					},
					{
						value: "#da251c",
						flag: false
					},
					{
						value: "#da6024",
						flag: false
					},
					{
						value: "#f29700",
						flag: false
					},
					{
						value: "#eeb41f",
						flag: false
					},
				],
				//背景色
				bgcolorList: [{
						value: "#fff",
						flag: true
					},
					{
						value: "#fae8bb",
						flag: false
					},
					{
						value: "#f7c166",
						flag: false
					},
					{
						value: "#e9a07c",
						flag: false
					},
					{
						value: "#e97c77",
						flag: false
					},
					{
						value: "#cf7275",
						flag: false
					},
					{
						value: "#8dac69",
						flag: false
					},
					{
						value: "#66988d",
						flag: false
					},
					{
						value: "#68adaf",
						flag: false
					},
					{
						value: "#a696aa",
						flag: false
					},
					{
						value: "#92bcee",
						flag: false
					},
					{
						value: "#9b9b9b",
						flag: false
					},
					{
						value: "#a0aebd",
						flag: false
					},
					{
						value: "#869fbe",
						flag: false
					},
					{
						value: "#6892c5",
						flag: false
					},
					{
						value: "#6785af",
						flag: false
					}
				]
			}
		},
		onShow() {
			setTimeout(() => {
				this.inputHandle()
			}, 400)
		},
		methods: {
			async inputHandle() {
				const res = await this.$refs.barcode.GetCodeImg()
				this.imgUrl = res.tempFilePath
				if (this.options.code == "") {
					this.imgUrl = ""
				}
			},
			//刷新
			refresh() {
				this.options.code = "E57890543271985";
				this.inputHandle()
			},
			//设置
			setHandle() {
				this.settingFlag = true;
			},
			//选择背景色
			bgHandle(val) {
				this.options.bgColor = val
				this.bgcolorList.forEach(item => {
					if (item.value == val) {
						item.flag = true
					} else {
						item.flag = false
					}
				})
				setTimeout(() => {
					this.inputHandle()
				}, 400)
			},
			//选择条形码颜色
			barColorHandle(val) {
				if (this.options.color.indexOf(val) == -1) {
					if (this.options.color.length > 9) {
						this.$refs.uToast.show({
							title: '最多选择10种颜色',
							type: 'error',
						})
						return
					}
					this.options.color.push(val)
					this.barColorList.forEach(item => {
						if (item.value == val) {
							item.flag = true
						}
					})
				} else {
					this.options.color = this.options.color.filter(item => item != val);
					this.barColorList.forEach(item => {
						if (item.value == val) {
							item.flag = false
						}
					})
				}
				console.log("颜色列表", this.options.color);
				setTimeout(() => {
					this.inputHandle()
				}, 400)
			},
			// 是否显示文字
			textSwitch() {
				if(this.showText){
					this.options.text.content = this.options.code
				}else{
					this.options.text.content = ""
				}
				setTimeout(() => {
					this.inputHandle()
				}, 400)
			},
			aleard(res) { // 条形码创建成功的回调 修改参数同样触发
				// console.log(res)
			},
			handleError(e) { //当发生错误时触发
				// console.log(e);
			},
			longtap(e) { //长安事件
				// console.log(e);
			},
			//图片预览
			prviewImg() {
				uni.previewImage({
					urls: [this.imgUrl],
					current: 1
				});
			},
		}
	}
</script>
<style lang="scss" scoped>
	.warpper {
		width: 100%;
		height: 100%;
		padding: 40rpx;
		box-sizing: border-box;
		z-index: 10;

		.img_container {
			width: 100%;
			height: 260rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			padding: 20rpx;
			padding-top: 40rpx;
			box-sizing: border-box;
			border-radius: 10rpx;
			background-color: #fff;
			box-shadow: 0 0 3px #000;
		}

		.refresh {
			width: 100%;
			padding: 40rpx 0;
			box-sizing: border-box;
			display: flex;
			justify-content: center;

			.btn {
				padding: 10rpx 20rpx;
				box-sizing: border-box;
				color: #fff;
				background: linear-gradient(to right, #0081ff, #1cbbb4);
				text-align: center;
				border-radius: 10rpx;
			}
		}

		.input_container {
			width: 100%;
			height: 80rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			margin-top: 20rpx;

			u-input {
				width: 100%;
				height: 100%;
				border: 1px solid #e7e7e7;
				border-radius: 20rpx;
				padding: 0 20rpx;
				box-sizing: border-box;
			}
		}

		.setting {
			width: 100%;
			padding: 40rpx 0;
			box-sizing: border-box;
			display: flex;
			justify-content: center;

			.btn {
				padding: 15rpx 20rpx;
				box-sizing: border-box;
				color: #fff;
				background: linear-gradient(to right, #39b54a, #8dc63f);
				text-align: center;
				border-radius: 10rpx;
			}
		}

		.set_content {
			padding: 40rpx;
			box-sizing: border-box;

			.set_item {
				margin: 40rpx 0;

				.set_title {
					display: flex;
					align-items: center;

					.title_label {
						width: 200rpx;
						font-weight: bold;
					}

					.title_content {
						flex: 1;
					}
				}

				.item_content {
					display: flex;
					justify-content: space-around;
					align-items: center;
					flex-wrap: wrap;
					margin-top: 20rpx;

					.color_li {
						width: 22%;
						height: 80rpx;
						margin-top: 20rpx;
						margin-bottom: 10rpx;
					}

					.shadow {
						box-shadow: 0 0 6px #000;
						border: 1px solid #fff;
					}
				}

			}
		}

		.barcode_container {
			position: absolute;
			left: 100%;
		}
	}
</style>